#include "ad7124.h"
#include "stdio.h"
#include "cmsis_os.h"



TEMPERATURE temperature;


/**
 * @brief 片选H
 */
void AD7124_CS_H()
{
	HAL_GPIO_WritePin(AD7124_CS_PORT, AD7124_CS_PIN, GPIO_PIN_SET);
}
/**
 * @brief 片选L
 */
void AD7124_CS_L()
{
	HAL_GPIO_WritePin(AD7124_CS_PORT, AD7124_CS_PIN, GPIO_PIN_RESET);
}





/**
 * @brief   AD7124写寄存器
 * @param   addr  寄存器地址
 * @param   *data 待写入数据
 * @param   data_size 待写入数据长度
 * @retval  成功返回0 其他失败
 */
HAL_StatusTypeDef AD7124_Write_Register(uint8_t addr, uint8_t *data, uint16_t data_size)
{
    uint8_t pData = AD7124_COMM_REG_WEN | AD7124_COMM_REG_WR | AD7124_COMM_REG_RA(addr);
    uint8_t ret;
    AD7124_CS_L();
    ret = HAL_SPI_Transmit(&AD7124_SPI_HANDLE, &pData, 1, 100);
    ret = HAL_SPI_Transmit(&AD7124_SPI_HANDLE, data, data_size, 100);
    AD7124_CS_H();
    return ret;
}




/**
 * @brief   AD7124读寄存器
 * @param   addr  寄存器地址
 * @param   *data 数据存储地址
 * @param   data_size 数据长度
 * @retval  成功返回0 其他失败
 */
HAL_StatusTypeDef AD7124_Read_Register(uint8_t addr, uint8_t *data, uint16_t data_size)
{
    uint8_t pData = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD | AD7124_COMM_REG_RA(addr);
    uint8_t ret=0;
    AD7124_CS_L();
    ret = HAL_SPI_Transmit(&AD7124_SPI_HANDLE, &pData, 1, 100);
    ret = HAL_SPI_Receive(&AD7124_SPI_HANDLE, data, data_size, 100);
    AD7124_CS_H();
    return ret;
}



/**
 * @brief  AD7124复位   连续写入64个1
 * @param  无
 * @retval 无
 */
void AD7124_Reset(void)
{
	uint8_t data=0xff;
    AD7124_CS_L();
    for(int i=0;i<8;i++)
    {
    	HAL_SPI_Transmit(&AD7124_SPI_HANDLE, &data, 1, 100);
    }
    AD7124_CS_H();
}


/**
 * @brief   开始单轮转换
 * @param   无
 * @retval  无
 * @note1   init函数后使用改函数进入单次转换模式(每次读取前都需调用)
 * @note2   AD7124_ADC_CTRL_REG(ADC控制寄存器)配置数据寄存器连续读取,功率模式,单次/连续转换模式,选择内部外部时钟等
 */
void Ad7124_Start_Single_Convert()
{
    uint8_t data[2];
    data[0] = (uint8_t)(0x5C4 >> 8);
    data[1] = (uint8_t)0x5C4;
	AD7124_Write_Register(AD7124_ADC_CTRL_REG,data,2);
}


/**
 * @brief   开始连续转换
 * @param   无
 * @retval  无
 * @note1   init函数后使用改函数进入连续转换模式
 * @note2   AD7124_ADC_CTRL_REG(ADC控制寄存器)配置数据寄存器连续读取,功率模式,单次/连续转换模式,选择内部外部时钟等
 */
void Ad7124_Start_Continuous_Convert()
{
    uint8_t data[2];
    data[0] = (uint8_t)(0x5C0 >> 8);
    data[1] = (uint8_t)0x5C0;
    AD7124_Write_Register(AD7124_ADC_CTRL_REG,data,2);
}



/**
 * @brief  24位有符号数转32位有符号数
 * @param  input待转换24位有符号数(如ADC芯片转换值)
 * @retval 32位有符号数转换结果
 */
int int24toint32(int input)
{
	if((input&0x800000)==0x800000)		//如果最高位为1，则是负数
	{
		input |= 0xff000000;		//高位补1
	}
	return input;
}


/**
 * @brief  AD7124初始化(单次一循环转换模式)，不可以就某一通道连续N次读取，进行取平均值等软件滤波
 * @param  无
 * @retval 无
 * @note0  初始化中使能所有需要的通道
 * @note1  初始化流程:复位->使能并配置转换通道(一个或多个通道)的输入->配置转换通道对应使用的CFG(x)寄存器+FILTER(x)寄存器
 * @note2  CFG(X),FILTER(X),OFFSET(X),GAIN(X)此四个寄存器芯片内部内部分组,捆绑使用(编号一致的为一组)
 * @note3  CFG(X)配置转换通道单/双极性,输入缓冲使能,参考电压选择,增益等
 * @note4  FILTER(X)滤波器类型,数据输出速率等
 * @note5  OFFSET(X)配置ADC芯片失调校准系数(本例程中保持上电默认值)
 * @note6  GAIN(X)配置满量程校准系数(本例程中保持上电默认值)
 */
void Ad7124_Init_Signle_Mode(void)
{
    uint8_t pData[3];

    AD7124_Reset(); // ADC复位
    HAL_Delay(100);


    pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN0<<5 | AD7124_AVSS) >> 8);//AIN0输入至AVSS,使用配置CFG0,转换通道CH0
    pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN0<<5 | AD7124_AVSS);
    AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
    HAL_Delay(10);


    pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN1<<5 | AD7124_AVSS) >> 8);//AIN1输入至AVSS,使用配置CFG0,转换通道CH1
    pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN1<<5 | AD7124_AVSS);
    AD7124_Write_Register(AD7124_CH1_MAP_REG, pData, 2);
    HAL_Delay(10);

    pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN2<<5 | AD7124_AVSS) >> 8);//AIN2输入至AVSS,使用配置CFG0,转换通道CH2
    pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN2<<5 | AD7124_AVSS);
    AD7124_Write_Register(AD7124_CH2_MAP_REG, pData, 2);
    HAL_Delay(10);

    pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN3<<5 | AD7124_AVSS) >> 8);//AIN3输入至AVSS,使用配置CFG0,转换通道CH3
    pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN3<<5 | AD7124_AVSS);
    AD7124_Write_Register(AD7124_CH3_MAP_REG, pData, 2);
    HAL_Delay(10);

    pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN4<<5 | AD7124_AVSS) >> 8);//AIN4输入至AVSS,使用配置CFG0,转换通道CH4
    pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN4<<5 | AD7124_AVSS);
    AD7124_Write_Register(AD7124_CH4_MAP_REG, pData, 2);
    HAL_Delay(10);

    pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN5<<5 | AD7124_AVSS) >> 8);//AIN5输入至AVSS,使用配置CFG0,转换通道CH5
    pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN5<<5 | AD7124_AVSS);
    AD7124_Write_Register(AD7124_CH5_MAP_REG, pData, 2);
    HAL_Delay(10);

    pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN6<<5 | AD7124_AVSS) >> 8);//AIN6输入至AVSS,使用配置CFG0,转换通道CH6
    pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN6<<5 | AD7124_AVSS);
    AD7124_Write_Register(AD7124_CH6_MAP_REG, pData, 2);
    HAL_Delay(10);

    pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN7<<5 | AD7124_AVSS) >> 8);//AIN7输入至AVSS,使用配置CFG0,转换通道CH7
    pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN7<<5 | AD7124_AVSS);
    AD7124_Write_Register(AD7124_CH7_MAP_REG, pData, 2);
    HAL_Delay(10);

    pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_20mV_P<<5 | AD7124_20mV_N) >> 8);//内部20mV基准,使用配置CFG0,转换通道CH9
    pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_20mV_P<<5 | AD7124_20mV_N);
    AD7124_Write_Register(AD7124_CH9_MAP_REG, pData, 2);
    HAL_Delay(10);

    pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG1<<12 | AD7124_INNER_TEMP<<5 | AD7124_AVSS) >> 8);	//内部温度传感器需要双极性输入,使用配置CFG1,转换通道CH10
    pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG1<<12 | AD7124_INNER_TEMP<<5 | AD7124_AVSS);
    AD7124_Write_Register(AD7124_CH10_MAP_REG, pData, 2);
    HAL_Delay(10);


    //所有单极性使用CFG0,CFG(X),FILTER(X),OFFSET(X),GAIN(X)此四个寄存器芯片内部分组,捆绑使用(编号一致的为一组)
    pData[0] = (uint8_t)(0x01F0 >> 8);
    pData[1] = (uint8_t)0x01F0;
    AD7124_Write_Register(AD7124_CFG0_REG, pData, 2); // 配置寄存器0(单极性)
    HAL_Delay(10);

    pData[0] = (uint8_t)(0x000010 >> 16);
    pData[1] = (uint8_t)(0x000010 >> 8);
    pData[2] = (uint8_t)(0x000010);
    AD7124_Write_Register(AD7124_FILT0_REG, pData, 3); // 滤波器寄存器0(bit0-bit10为数据输出速率设置(FS))
    HAL_Delay(10);

    //所有双极性使用CFG1,CFG(X),FILTER(X),OFFSET(X),GAIN(X)此四个寄存器芯片内部分组,捆绑使用(编号一致的为一组)
    pData[0] = (uint8_t)(0x09F0 >> 8);
    pData[1] = (uint8_t)0x09F0;
    AD7124_Write_Register(AD7124_CFG1_REG, pData, 2); // 配置寄存器1(双极性)
    HAL_Delay(10);
    pData[0] = (uint8_t)(0x000010 >> 16);
    pData[1] = (uint8_t)(0x000010 >> 8);
    pData[2] = (uint8_t)(0x000010);
    AD7124_Write_Register(AD7124_FILT1_REG, pData, 3); // 滤波器寄存器1(bit0-bit10为数据输出速率设置(FS))
    HAL_Delay(10);
}




/**
 * @brief  AD7124初始化(单通道循环转换模式)，每次读取根据需要配置输入通道至转换通道，较为灵活，可以单通道连续读取N次，来软件滤波
 * @param  无
 * @retval 无
 * @note0  读取函数中使能所有需要的通道
 * @note1  初始化流程:复位->配置转换通道对应使用的CFG(x)寄存器+FILTER(x)寄存器
 * @note2  CFG(X),FILTER(X),OFFSET(X),GAIN(X)此四个寄存器芯片内部分组,捆绑使用(编号一致的为一组)
 * @note3  CFG(X)配置转换通道单/双极性,输入缓冲使能,参考电压选择,增益等
 * @note4  FILTER(X)滤波器类型,数据输出速率等
 * @note5  OFFSET(X)配置ADC芯片失调校准系数(本例程中保持上电默认值)
 * @note6  GAIN(X)配置满量程校准系数(本例程中保持上电默认值)
 */
void Ad7124_Init_Continuous(void)
{
    uint8_t pData[3];

    AD7124_Reset(); // ADC复位
    HAL_Delay(100);

    //配置CFG0单极性,CFG(X),FILTER(X),OFFSET(X),GAIN(X)此四个寄存器芯片内部分组,捆绑使用(编号一致的为一组)
    pData[0] = (uint8_t)(0x01F0 >> 8);//单极性,开启缓冲,内部基准,增益1
    pData[1] = (uint8_t)0x01F0;
    AD7124_Write_Register(AD7124_CFG0_REG, pData, 2); // 配置寄存器0(单极性)
    HAL_Delay(10);

    pData[0] = (uint8_t)(0x000010 >> 16);
    pData[1] = (uint8_t)(0x000010 >> 8);
    pData[2] = (uint8_t)(0x000010);
    AD7124_Write_Register(AD7124_FILT0_REG, pData, 3); // 滤波器寄存器0(bit0-bit10为数据输出速率设置(FS))
    HAL_Delay(10);

    //配置CFG1双极性,CFG(X),FILTER(X),OFFSET(X),GAIN(X)此四个寄存器芯片内部分组,捆绑使用(编号一致的为一组)
    pData[0] = (uint8_t)(0x09F0 >> 8);//双极性,开启缓冲,内部基准,增益1
    pData[1] = (uint8_t)0x09F0;
    AD7124_Write_Register(AD7124_CFG1_REG, pData, 2); // 配置寄存器1(双极性)
    HAL_Delay(10);
    pData[0] = (uint8_t)(0x000010 >> 16);
    pData[1] = (uint8_t)(0x000010 >> 8);
    pData[2] = (uint8_t)(0x000010);
    AD7124_Write_Register(AD7124_FILT1_REG, pData, 3); // 滤波器寄存器1(bit0-bit10为数据输出速率设置(FS))
    HAL_Delay(10);

    Ad7124_Start_Continuous_Convert();
}



/**
 * @brief  单伦转换，一次读取一轮，AD7124读取ADC值(配合Ad7124_Start_Single_Convert，一次性顺序读取(一轮)init函数中使能的通道)
 * @param  *data数据缓存(双极性需要单独处理)
 * @param  num使能的通道数量,必须等于Ad7124_Init_Signle_Mode中使能的通道数量
 * @retval 0读取成功,1读取错误,3读取超时
 * @note1  初始化函数中***需将所有需要使用的通道使能***,CFG配置好(重要),每个CFG与各自FILTER对应(影响转换速率)
 * @note2  单个通道转换+读取时间约4mS
 * @note3  使用双极性配置的通道,读取到的值需要单独处理
 */
uint8_t Ad7124_Read_Signle_Mode(float *data,uint8_t num)
{
    uint8_t rData[4];
    uint32_t rData32;
	uint8_t pData;
	uint8_t count=0;

	Ad7124_Start_Single_Convert();

	pData = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD | AD7124_COMM_REG_RA(AD7124_DATA_REG);

	AD7124_CS_L();
    for(int i=0;i<num;i++)
    {
		while(HAL_GPIO_ReadPin(AD7124_DRDY_PORT, AD7124_DRDY_PIN)==1)
		{
			count++;
			if(count>100)
			{
				AD7124_CS_H();
				return HAL_TIMEOUT;
			}
			HAL_Delay(1);
		}

		HAL_SPI_Transmit(&hspi1, &pData, 1, 100);
		HAL_SPI_Receive(&hspi1, rData, 4, 100);


		rData32 = (rData[0] << 16)  | (rData[1] << 8)  | rData[2];

		//双极性通道需要单独处理
		switch(rData[3]&0x0f)
		{
			case 0 :  data[0]= (rData32 * 2.5) / 16777215;
					  break;
			case 1 :  data[1]= (rData32 * 2.5) / 16777215;
					  break;
			case 2 :  data[2]= (rData32 * 2.5) / 16777215;
					  break;
			case 3 :  data[3]= (rData32 * 2.5) / 16777215;
					  break;
			case 4 :  data[4]= (rData32 * 2.5) / 16777215;
					  break;
		    case 5 :  data[5]= (rData32 * 2.5) / 16777215;
					  break;
			case 6 :  data[6]= (rData32 * 2.5) / 16777215;
					  break;
			case 7 :  data[7]= (rData32 * 2.5) / 16777215;
					  break;
			case 8 :  data[8]= (rData32 * 2.5) / 16777215;
					  break;
			case 9 :  data[9]= (rData32 * 2.5) / 16777215;
					  break;
			case 10 : data[10]= (int24toint32(rData32)-int24toint32(0x800000))/13584.0f-272.5;//1.双极性输出的通道需要单独处理,2.内部温度转换需要单独处理
					  break;
			case 11 : data[11]= (rData32 * 2.5) / 16777215;
					  break;
			case 12 : data[12]= (rData32 * 2.5) / 16777215;
					  break;
			case 13 : data[13]= (rData32 * 2.5) / 16777215;
					  break;
			case 14 : data[14]= (rData32 * 2.5) / 16777215;
					  break;
			case 15 : data[15]= (rData32 * 2.5) / 16777215;
					  break;
			default : AD7124_CS_H();
					  return HAL_ERROR;
		}
    }
    AD7124_CS_H();
    return HAL_OK;
}






//***************************************使用CH0转换通道，连续转换读取如(AIN0,AIN1,内部20mV基准,内部温度)****************//
//************************************************初始化只能使能CH0,CFG0,FILTER0**************************//
//************************************************只能使用连续转换模式*************************************//



/**
 * @brief  连续转换，单通道读取(设置任意一个输入至CH0,等待转换完成,读取一次转换结果)
 * @param  *data数据缓存
 * @retval 0读取成功,3读取超时
 * @note1  初始化函数中只能使用CH0,CFG0,FILTER0
 * @note2  单个通道转换+读取时间约4mS
 */
uint8_t Ad7124_Read_Continuous_Mode(float *data,uint8_t channel)
{
    uint8_t rData[4];
    uint32_t rData32;
    uint8_t pData[3];
    uint8_t count=0;

	switch(channel)
	{
		case 0  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN0<<5 | AD7124_AVSS) >> 8);		//AIN0输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN0<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 1  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN1<<5 | AD7124_AVSS) >> 8);		//AIN1输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN1<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 2  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN2<<5 | AD7124_AVSS) >> 8);		//AIN2输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN2<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 3  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN3<<5 | AD7124_AVSS) >> 8);		//AIN3输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN3<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 4  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN4<<5 | AD7124_AVSS) >> 8);		//AIN4输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN4<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 5  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN5<<5 | AD7124_AVSS) >> 8);		//AIN5输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN5<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 6  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN6<<5 | AD7124_AVSS) >> 8);		//AIN6输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN6<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 7  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN7<<5 | AD7124_AVSS) >> 8);		//AIN7输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN7<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 8  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN8<<5 | AD7124_AVSS) >> 8);		//AIN8输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN8<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 9  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN9<<5 | AD7124_AVSS) >> 8);		//AIN9输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN9<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 10  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN10<<5 | AD7124_AVSS) >> 8);		//AIN10输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN10<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 11  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN11<<5 | AD7124_AVSS) >> 8);		//AIN11输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN11<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 12  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN12<<5 | AD7124_AVSS) >> 8);		//AIN12输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN12<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 13  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN13<<5 | AD7124_AVSS) >> 8);		//AIN13输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN13<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 14  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN14<<5 | AD7124_AVSS) >> 8);		//AIN14输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN14<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;
		case 15  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN15<<5 | AD7124_AVSS) >> 8);		//AIN15输入至AVSS,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_AIN15<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;

		case 16  :
//					pData[0] = (uint8_t)(0x01F0 >> 8);								//单极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x01F0;
//					AD7124_Write_Register(AD7124_CFG0_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG0<<12 | AD7124_20mV_P<<5 | AD7124_20mV_N) >> 8);		//内部20mV基准,使用CH0+CFG0
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG0<<12 | AD7124_20mV_P<<5 | AD7124_20mV_N);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;

		case 17  :
//					pData[0] = (uint8_t)(0x09F0 >> 8);								//双极性,开启缓冲,内部基准,增益1
//					pData[1] = (uint8_t)0x09F0;
//					AD7124_Write_Register(AD7124_CFG1_REG, pData, 2);
//					HAL_Delay(1);
					pData[0] = (uint8_t)((0x8000 | AD7124_USE_CFG1<<12 | AD7124_INNER_TEMP<<5 | AD7124_AVSS) >> 8);		//内部温度,使用CH0+CFG1
					pData[1] = (uint8_t)(0x8000 | AD7124_USE_CFG1<<12 | AD7124_INNER_TEMP<<5 | AD7124_AVSS);
					AD7124_Write_Register(AD7124_CH0_MAP_REG, pData, 2);
//					HAL_Delay(1);
					break;

		default : break;
	}

	HAL_Delay(1);//等待设置的通道生效，不然会读取到上次的转换结果

    AD7124_CS_L();
	while(HAL_GPIO_ReadPin(AD7124_DRDY_PORT, AD7124_DRDY_PIN)==1)
	{
		count++;
		if(count>100)
		{
			AD7124_CS_H();
			return HAL_TIMEOUT;
		}
		HAL_Delay(1);
	}
	pData[0] = AD7124_COMM_REG_WEN | AD7124_COMM_REG_RD | AD7124_COMM_REG_RA(AD7124_DATA_REG);
	HAL_SPI_Transmit(&hspi1, pData, 1, 100);
	HAL_SPI_Receive(&hspi1, rData, 4, 100);

	AD7124_CS_H();

	rData32 = (rData[0] << 16)  | (rData[1] << 8)  | rData[2];

	if(channel==17)//双极性单独处理
	{
		*data=(int24toint32(rData32)-int24toint32(0x800000))/13584.0f-272.5;
	}else {
		*data = (rData32 * 2.5) / 16777215;
	}

	return HAL_OK;
}




//***************************************使用CH0转换通道，连续转换读取如(AIN0,AIN1,内部20mV基准,内部温度)****************//
//************************************************初始化只能使能CH0,CFG0,FILTER0**************************//
//************************************************只能使用连续转换模式*************************************//

















//***************************************IO口输出&激励电流输出&偏置电压输出**********************************//


/**
 * @brief  IO口输出使能
 * @param  io_channel 需要输出的IO口(共四个)
 * @note   可与该输入引脚采集功能同时使能(采集精度会下降)
 */
void AD7124_IO_Output_Enable(AD7124_IO_CHANNEL io_channel)
{
	AD7124_IO_CONTROL io_control_data;
	AD7124_Read_Register(AD7124_IO_CTRL1_REG,io_control_data.data,3);
	switch(io_channel)
	{
		case AD7124_IO_AIN2	: io_control_data.reg.gpio_ctrl1=ENABLE;break;
		case AD7124_IO_AIN3	: io_control_data.reg.gpio_ctrl2=ENABLE;break;
		case AD7124_IO_AIN4	: io_control_data.reg.gpio_ctrl3=ENABLE;break;
		case AD7124_IO_AIN5	: io_control_data.reg.gpio_ctrl4=ENABLE;break;
		case AD7124_IO_ALL  : io_control_data.reg.gpio_ctrl1=ENABLE;
							  io_control_data.reg.gpio_ctrl2=ENABLE;
							  io_control_data.reg.gpio_ctrl3=ENABLE;
							  io_control_data.reg.gpio_ctrl4=ENABLE;break;
		default : return;
	}
    AD7124_Write_Register(AD7124_IO_CTRL1_REG, io_control_data.data, 3);
}

/**
 * @brief  IO口输出关闭
 * @param  io_channel 需要输出的IO口(共四个)
 */
void AD7124_IO_Output_Disable(AD7124_IO_CHANNEL io_channel)
{
	AD7124_IO_CONTROL io_control_data;
	AD7124_Read_Register(AD7124_IO_CTRL1_REG,io_control_data.data,3);
	switch(io_channel)
	{
		case AD7124_IO_AIN2	: io_control_data.reg.gpio_ctrl1=DISABLE;break;
		case AD7124_IO_AIN3	: io_control_data.reg.gpio_ctrl2=DISABLE;break;
		case AD7124_IO_AIN4	: io_control_data.reg.gpio_ctrl3=DISABLE;break;
		case AD7124_IO_AIN5	: io_control_data.reg.gpio_ctrl4=DISABLE;break;
		case AD7124_IO_ALL  : io_control_data.reg.gpio_ctrl1=DISABLE;
							  io_control_data.reg.gpio_ctrl2=DISABLE;
							  io_control_data.reg.gpio_ctrl3=DISABLE;
							  io_control_data.reg.gpio_ctrl4=DISABLE;break;
		default : return;
	}
    AD7124_Write_Register(AD7124_IO_CTRL1_REG, io_control_data.data, 3);
}

/**
 * @brief  设置IO口输出状态
 * @param  io_channel 需要输出的IO口(共四个)
 * @param  state电平状态
 * @note   可与该输入引脚采集功能同时使能(采集精度会下降)
 */
void AD7124_SetIO_State(AD7124_IO_CHANNEL io_channel,uint8_t state)
{
	AD7124_IO_CONTROL io_control_data;
	AD7124_Read_Register(AD7124_IO_CTRL1_REG,io_control_data.data,3);
	switch(io_channel)
	{
		case AD7124_IO_AIN2	: io_control_data.reg.gpio_data1=state;break;
		case AD7124_IO_AIN3	: io_control_data.reg.gpio_data2=state;break;
		case AD7124_IO_AIN4	: io_control_data.reg.gpio_data3=state;break;
		case AD7124_IO_AIN5	: io_control_data.reg.gpio_data4=state;break;
		case AD7124_IO_ALL  : io_control_data.reg.gpio_data1=state;
							  io_control_data.reg.gpio_data2=state;
							  io_control_data.reg.gpio_data3=state;
							  io_control_data.reg.gpio_data4=state;break;
		default : return;
	}
    AD7124_Write_Register(AD7124_IO_CTRL1_REG, io_control_data.data, 3);
}


/**
 * @brief  激励电流横流源输出使能
 * @param  ioutx 两个恒流源中的一个
 * @param  current横流电流大小(最大提供AVDD电压)
 * @param  iout_channel 电流输出口
 * @note   可与该输入引脚采集功能同时使能(采集精度会下降)
 */
void AD7124_IOUT_Enable(AD7124_IOUTX ioutx,AD7124_IOUT_CURRENT current,AD7124_AIN_CHANNEL iout_channel)
{
	AD7124_IO_CONTROL io_control_data;
	AD7124_Read_Register(AD7124_IO_CTRL1_REG,io_control_data.data,3);
	switch(ioutx)
	{
		case IOUT0  : io_control_data.reg.iout0=current;
					  io_control_data.reg.iout0_ch=iout_channel;
					  break;
		case IOUT1  : io_control_data.reg.iout1=current;
					  io_control_data.reg.iout1_ch=iout_channel;
					  break;
		default : return;
	}
    AD7124_Write_Register(AD7124_IO_CTRL1_REG, io_control_data.data, 3);
}

/**
 * @brief  激励电流横流源输出关闭
 * @param  ioutx 两个恒流源中的一个
 */
void AD7124_IOUT_Disable(AD7124_IOUTX ioutx)
{
	AD7124_IO_CONTROL io_control_data;
	AD7124_Read_Register(AD7124_IO_CTRL1_REG,io_control_data.data,3);
	switch(ioutx)
	{
		case IOUT0  : io_control_data.reg.iout0=0;
					  break;
		case IOUT1  : io_control_data.reg.iout1=0;
					  break;
		default : return;
	}
    AD7124_Write_Register(AD7124_IO_CTRL1_REG, io_control_data.data, 3);
}


/**
 * @brief  使能AVDD/2偏置电压输出
 * @param  iout_channel电压输出口
 * @note   可与该输入引脚采集功能同时使能(采集精度会下降)
 * @note   该偏置电压带负载能力很弱(负载阻抗需要20K以上),一般在热电偶应用中使用
 */
void AD7124_VBIAS_Enable(AD7124_AIN_CHANNEL iout_channel)
{
	uint8_t data[2];
	uint16_t temp=1<<iout_channel;
	AD7124_Read_Register(AD7124_IO_CTRL2_REG,data,2);
	data[0]|=(uint8_t)(temp>>8);
	data[1]|=(uint8_t)(temp&0x0f);
	AD7124_Write_Register(AD7124_IO_CTRL2_REG, data, 2);
}

/**
 * @brief  关闭AVDD/2偏置电压输出
 * @param  iout_channel电压输出口
 */
void AD7124_VBIAS_Disable(AD7124_AIN_CHANNEL iout_channel)
{
	uint8_t data[2];
	uint16_t temp=~(1<<iout_channel);
	AD7124_Read_Register(AD7124_IO_CTRL2_REG,data,2);
	data[0]&=(uint8_t)(temp>>8);
	data[1]&=(uint8_t)(temp&0x0f);
	AD7124_Write_Register(AD7124_IO_CTRL2_REG, data, 2);
}


//***************************************IO口输出&激励电流输出&偏置电压输出**********************************//








void ADC_Data_Convert()
{
	for(int i=0;i<8;i++)
	{
		if(temperature.adc_data[i]==2.5)
		{
			temperature.temp[i]=9999;
		}else {
			temperature.temp[i]=((1500.0f*temperature.adc_data[i])/(2.5-temperature.adc_data[i])-1000.0f)/3.85;
		}
	}
}



